package com.bawei.mdm2207alast.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Badge
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.R
import com.bawei.mdm2207alast.route.RouteConfig
import com.blankj.utilcode.util.ToastUtils

/**
 * 作者：赵亮
 * 时间：2024/12/16 14:33
 * 功能：
 */

/**
 * 入口菜单数据类
 * @param 菜单图片
 * @param 菜单标题文字
 */
data class HomeEntity(val img: Int, val title: String)

@Composable
fun HomePage(navController: NavController = rememberNavController()) {

    // 菜单1 数据
    val list1 = listOf(
        HomeEntity(R.drawable.icon32, "在线应用"),
        HomeEntity(R.drawable.icon32, "本地应用"),
        HomeEntity(R.drawable.icon1912, "考勤打卡"),
        HomeEntity(R.drawable.icon81, "智能审批"),
        HomeEntity(R.drawable.icon359, "工作日志"),
        HomeEntity(R.drawable.icon105, "任务"),
        HomeEntity(R.drawable.icon3, "文件"),
    )

    // 菜单2 数据
    val list2 = listOf(
        HomeEntity(R.drawable.icon1887, "联系人"),
        HomeEntity(R.drawable.icon980, "讨论组"),
        HomeEntity(R.drawable.icon1907, "主旨架构"),
    )

    Column {
        // 标题
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .fillMaxWidth()
                .height(70.dp)
                .background(color = colorResource(id = R.color.purple_200))
        ) {
            Spacer(modifier = Modifier.width(15.dp))
            Badge(containerColor = Color.Green, modifier = Modifier.size(25.dp)) {
                Text(text = "12", color = Color.White)
            }
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "主门户")
        }

        Box(modifier = Modifier.fillMaxSize()) {
            // 背景
            Image(
                modifier = Modifier.fillMaxWidth(),
                contentScale = ContentScale.Crop,
                painter = painterResource(id = R.drawable.icon1516),
                contentDescription = null
            )
            // 菜单
            Column {
                Spacer(modifier = Modifier.height(5.dp))
                // 菜单1
                SecondTitle(R.drawable.icon814, "应用")
                Spacer(modifier = Modifier.height(5.dp))
                LazyVerticalGrid(
                    columns = GridCells.Fixed(3),
                    verticalArrangement = Arrangement.spacedBy(10.dp)
                ) {
                    items(list1) {
                        Column(
                            modifier = Modifier.clickable {
                                when (it.title) {
                                    "在线应用" -> navController.navigate(RouteConfig.ONLINEAPPS)
                                    "本地应用" -> navController.navigate(RouteConfig.LOCALAPPS)
                                    "考勤打卡" -> navController.navigate(RouteConfig.SELSIGN)
                                    "智能审批" -> {
                                        ToastUtils.showShort("功能开发中......")
                                    }

                                    "工作日志" -> navController.navigate(RouteConfig.SELREPORT)
                                    "任务" -> {
                                        ToastUtils.showShort("功能开发中......")
                                    }

                                    "文件" -> navController.navigate(RouteConfig.ONLINEFILES)
                                }
                            },
                            verticalArrangement = Arrangement.spacedBy(5.dp),
                            horizontalAlignment = Alignment.CenterHorizontally
                        ) {
                            Image(
                                modifier = Modifier.size(30.dp),
                                painter = painterResource(id = it.img),
                                contentDescription = null
                            )
                            Text(text = it.title)
                        }
                    }
                }
                // 菜单2
                Spacer(modifier = Modifier.height(5.dp))
                SecondTitle(R.drawable.icon1488, "通信")
                Spacer(modifier = Modifier.height(5.dp))
                LazyVerticalGrid(columns = GridCells.Fixed(3)) {
                    items(list2) {
                        Column(
                            verticalArrangement = Arrangement.spacedBy(5.dp),
                            horizontalAlignment = Alignment.CenterHorizontally
                        ) {
                            Image(
                                painter = painterResource(id = it.img),
                                contentDescription = null,
                                modifier = Modifier.size(30.dp)
                            )
                            Text(text = it.title)
                        }
                    }
                }
                // 菜单3
                Spacer(modifier = Modifier.height(5.dp))
                SecondTitle(R.drawable.icon352, "公众号")
            }
        }
    }

}

@Composable
private fun SecondTitle(img: Int, text: String) {
    Row(verticalAlignment = Alignment.CenterVertically) {
        Spacer(modifier = Modifier.width(15.dp))
        Image(
            modifier = Modifier.size(23.dp),
            painter = painterResource(id = img),
            contentDescription = null
        )
        Text(text = text)
    }
}

@Preview(showSystemUi = true)
@Composable
fun HomePagePreview() {
    HomePage()
}

